package com.trailer.base.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.trailer.base.R;
import com.trailer.base.base.BAdapter;

import java.util.ArrayList;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;

public class DialogHelper {

    private Dialog dialog;
    private SparseArray<View> views;
    private Context context;


    public DialogHelper(Context context) {
        dialog = new Dialog(context);
        Window window = dialog.getWindow();
        window.setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.windowAnimations = R.style.dialogAnimCenter;
        window.setAttributes(lp);
    }

    public DialogHelper setContentView(@LayoutRes int layoutId) {
        this.views = new SparseArray<>();
        dialog.setContentView(layoutId);
        return this;
    }

    public DialogHelper setContentView(View contentView) {
        this.views = new SparseArray<>();
        dialog.setContentView(contentView);
        return this;
    }

    public DialogHelper show() {
        dialog.show();
        return this;
    }

    public void dismiss() {
        if (dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
            context = null;
        }
    }

    public DialogHelper setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        dialog.setOnDismissListener(onDismissListener);
        return this;
    }

    /**
     * Will set the text of a TextView.
     *
     * @param viewId The view id.
     * @param value The text to put in the text view.
     *
     * @return The BViewHolder for chaining.
     */
    public DialogHelper setText(@IdRes int viewId, CharSequence value) {
        TextView view = getView(viewId);
        view.setText(value);
        return this;
    }


    public DialogHelper setText(@IdRes int viewId, @StringRes int strId) {
        TextView view = getView(viewId);
        view.setText(strId);
        return this;
    }


    public DialogHelper setHint(@IdRes int viewId, CharSequence value) {
        TextView view = getView(viewId);
        view.setHint(value);
        return this;
    }

    public DialogHelper setHint(@IdRes int viewId, @StringRes int strId) {
        TextView view = getView(viewId);
        view.setHint(strId);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T getView(@IdRes int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = dialog.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }


    /**
     * Will set the image of an ImageView from a resource id.
     *
     * @param viewId The view id.
     * @param imageResId The image resource id.
     *
     * @return The BViewHolder for chaining.
     */
    public DialogHelper setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return this;
    }

    /**
     * Will set background color of a view.
     *
     * @param viewId The view id.
     * @param color A color, not a resource id.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setBackgroundColor(@IdRes int viewId, @ColorInt int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    /**
     * Will set background of a view.
     *
     * @param viewId The view id.
     * @param backgroundRes A resource to use as a background.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setBackgroundRes(@IdRes int viewId, @DrawableRes int backgroundRes) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    /**
     * Will set text color of a TextView.
     *
     * @param viewId The view id.
     * @param textColor The text color (not a resource id).
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setTextColor(@IdRes int viewId, @ColorInt int textColor) {
        TextView view = getView(viewId);
        view.setTextColor(textColor);
        return this;
    }


    /**
     * Will set the image of an ImageView from a drawable.
     *
     * @param viewId The view id.
     * @param drawable The image drawable.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setImageDrawable(@IdRes int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    /**
     * Add an action to set the image of an image view. Can be called multiple times.
     */
    public DialogHelper setImageBitmap(@IdRes int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    /**
     * Add an action to set the alpha of a view. Can be called multiple times.
     * Alpha between 0-1.
     */
    public DialogHelper setAlpha(@IdRes int viewId, float value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getView(viewId).setAlpha(value);
        } else {
            // Pre-honeycomb hack to set Alpha value
            AlphaAnimation alpha = new AlphaAnimation(value, value);
            alpha.setDuration(0);
            alpha.setFillAfter(true);
            getView(viewId).startAnimation(alpha);
        }
        return this;
    }

    /**
     * Set a view visibility to VISIBLE (true) or GONE (false).
     *
     * @param viewId The view id.
     * @param visible True for VISIBLE, false for GONE.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setGone(@IdRes int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    /**
     * Set a view visibility to VISIBLE (true) or INVISIBLE (false).
     *
     * @param viewId The view id.
     * @param visible True for VISIBLE, false for INVISIBLE.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setVisible(@IdRes int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    /**
     * Add links into a TextView.
     *
     * @param viewId The id of the TextView to linkify.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper linkify(@IdRes int viewId) {
        TextView view = getView(viewId);
        Linkify.addLinks(view, Linkify.ALL);
        return this;
    }

    /**
     * Apply the typeface to the given viewId, and enable subpixel rendering.
     */
    public DialogHelper setTypeface(@IdRes int viewId, Typeface typeface) {
        TextView view = getView(viewId);
        view.setTypeface(typeface);
        view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        return this;
    }

    /**
     * Apply the typeface to all the given viewIds, and enable subpixel rendering.
     */
    public DialogHelper setTypeface(Typeface typeface, int... viewIds) {
        for (int viewId : viewIds) {
            TextView view = getView(viewId);
            view.setTypeface(typeface);
            view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }

    /**
     * Sets the progress of a ProgressBar.
     *
     * @param viewId The view id.
     * @param progress The progress.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setProgress(@IdRes int viewId, int progress) {
        ProgressBar view = getView(viewId);
        view.setProgress(progress);
        return this;
    }

    /**
     * Sets the progress and max of a ProgressBar.
     *
     * @param viewId The view id.
     * @param progress The progress.
     * @param max The max value of a ProgressBar.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setProgress(@IdRes int viewId, int progress, int max) {
        ProgressBar view = getView(viewId);
        view.setMax(max);
        view.setProgress(progress);
        return this;
    }

    /**
     * Sets the range of a ProgressBar to 0...max.
     *
     * @param viewId The view id.
     * @param max The max value of a ProgressBar.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setMax(@IdRes int viewId, int max) {
        ProgressBar view = getView(viewId);
        view.setMax(max);
        return this;
    }

    /**
     * Sets the rating (the number of stars filled) of a RatingBar.
     *
     * @param viewId The view id.
     * @param rating The rating.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setRating(@IdRes int viewId, float rating) {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return this;
    }

    /**
     * Sets the rating (the number of stars filled) and max of a RatingBar.
     *
     * @param viewId The view id.
     * @param rating The rating.
     * @param max The range of the RatingBar to 0...max.
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setRating(@IdRes int viewId, float rating, int max) {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    /**
     * Sets the on click listener of the view.
     *
     * @param viewId The view id.
     * @param listener The on click listener;
     *
     * @return The DialogHelper for chaining.
     */
    public DialogHelper setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }


    public DialogHelper setAdapter(@IdRes int viewId, BAdapter adapter) {
        RecyclerView view = getView(viewId);
        if (view != null) {
            view.setAdapter(adapter);
        }
        return this;
    }

    public DialogHelper setAdapterData(@IdRes int viewId, ArrayList data) {
        RecyclerView view = getView(viewId);
        if (view != null) {
            ((BAdapter) view.getAdapter()).setNewData(data);
        }
        return this;
    }




}
